package com.biao.mall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biao.common.utils.PageUtils;
import com.biao.common.utils.Query;
import com.biao.common.utils.R;
import com.biao.mall.ware.dao.WareInfoDao;
import com.biao.mall.ware.entity.WareInfoEntity;
import com.biao.mall.ware.feign.MemberFeignService;
import com.biao.mall.ware.service.WareInfoService;
import com.biao.mall.ware.vo.FareVo;
import com.biao.mall.ware.vo.MemberAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Random;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WareInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        queryWrapper.and(StringUtils.isNotBlank(key), wrapper ->
                wrapper.like(WareInfoEntity::getName, key).or()
                        .eq(WareInfoEntity::getId, key).or()
                        .like(WareInfoEntity::getAddress, key).or()
                        .like(WareInfoEntity::getAreacode, key)

        );

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.info(addrId);
        MemberAddressVo memberAddressVo = r.getData2("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
        });
        if (memberAddressVo != null) {
            fareVo.setAddress(memberAddressVo);
            fareVo.setFare(new BigDecimal("10.20"));
            return fareVo;
        }
        fareVo.setFare(new BigDecimal("-1"));
        return fareVo;
    }

}